package com.wsc.goods.controller;

import com.wsc.goods.domain.GoodsTypePo;
import com.wsc.goods.service.GoodsTypeService;
import com.wsc.tools.AjaxResult;
import com.wsc.tools.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class GoodsTypeController {

    @Autowired
    GoodsTypeService goodsTypeService;

    /**
     * 修改节点
     * @param po 修改对象
     * @return 是否成功
     */
    @PostMapping("/modNode")
    public AjaxResult modNode(@RequestBody GoodsTypePo po){
        try {
            goodsTypeService.updateById(po);
            return AjaxResult.getInstance().setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getInstance().setMsg("修改失败").setCode(500);
        }
    }

    /**
     * 添加节点
     */
    @PostMapping("/addNode")
    public AjaxResult addNode (@RequestBody GoodsTypePo po){
        try {
            if(po.getPid() == -1){
                po.setPid(0L);
                po.setId(0L);
                po.setSortIndex(100);
                po.setTotalCount(0);
                po.setSeoKeywords(po.getName());
                po.setSeoTitle(po.getName());
                po.setCreateTime(System.currentTimeMillis());
                goodsTypeService.addNode(po);
                po.setId(po.getId());
                po.setPath("."+po.getId()+".");
                goodsTypeService.updateById(po);
            }else {
                po.setPid(po.getId());
                po.setId(0L);
                po.setSortIndex(100);
                po.setTotalCount(0);
                po.setSeoKeywords(po.getName());
                po.setSeoTitle(po.getName());
                po.setCreateTime(System.currentTimeMillis());
                goodsTypeService.addNode(po);
                po.setPath(po.getPath()+po.getId()+".");
                goodsTypeService.updateById(po);
            }
            return AjaxResult.getInstance().setMsg("新增成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getInstance().setMsg("新增失败").setCode(500);
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @GetMapping("/delType/{ids}")
    public AjaxResult delBrand(@PathVariable("ids") String ids){
        List<Long> arrs = StrUtils.splitStr2LongArr(ids);
        try {
            goodsTypeService.removeByIds(arrs);
            return AjaxResult.getInstance().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getInstance().setCode(500).setMsg("删除失败");
        }

    }

    /**
     * 加载树
     * @return
     */
    @PostMapping("/getTree")
    public List<GoodsTypePo> getTree(@RequestBody GoodsTypePo po){
        return goodsTypeService.getTree(po);
    }

    /**
     * 加载模板
     * @return 是否成功
     */
    @GetMapping("/create")
    public AjaxResult createTemplate(){
        return goodsTypeService.createTemplate();
    }

    /**
     * 加载模板
     * @return 是否成功
     */
    @GetMapping("/getPath/{id}")
    public String getPath(@PathVariable("id") Long id){
            GoodsTypePo gp= goodsTypeService.getById(id);
            return gp.getPath();
    }

}
